package com.bdqn.zhang.action;

import com.alibaba.fastjson.JSON;
import com.bdqn.zhang.entity.SysUser;
import com.bdqn.zhang.service.SysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by java on 2016/5/13.
 */

public class SysUserAction extends ActionSupport implements ModelDriven<SysUser> {

    private SysUser sysuser = new SysUser();

    @Override
    public SysUser getModel() {
        return sysuser;
    }


    private SysUserService sysUserService;

    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    private File file;
    private String fileFileName;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public Integer page;

    public Integer limit;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    /**
     * 登陆
     * @return
     * @throws Exception
     */
    public String login() throws Exception {
        SysUser user = sysUserService.findUser(sysuser);
        if(user != null){
            /*存储用户信息到session*/
            //TODO
            Map session = ActionContext.getContext().getSession();
            session.put("user",user);
            result = "true";
            return SUCCESS;
        } else {
            result = "false";
            return SUCCESS;
        }
    }

    /**
     * 跳到index
     * @return
     * @throws Exception
     */
    public String toindex() throws Exception {
        System.out.println("index");
        return SUCCESS;
    }

    /**
     * 文件上传
     * @return
     * @throws Exception
     */
    public String upload() throws Exception {


        System.out.println("index");

        byte[] buffer=new byte[1024];
        FileInputStream fis=new FileInputStream(file);
        String str = UUID.randomUUID().toString().replaceAll("-","");
        String fileType = fileFileName.substring(fileFileName.lastIndexOf("."));
        FileOutputStream fos=new FileOutputStream("D:/upload/"+str+fileType);
        int length=fis.read(buffer);
        while (length>0) {
            //每次写入length长度的内容
            fos.write(buffer,0, length);
            length=fis.read(buffer);
        }
        fis.close();
        fos.flush();
        fos.close();
        return SUCCESS;
    }



    public String touser() throws Exception {
        System.out.println("index");
        return SUCCESS;
    }

    public String house() throws Exception {
        System.out.println("index");
        return SUCCESS;
    }

    public String userlist() throws Exception {
        List<SysUser> userlist = sysUserService.findAllUser(page,limit);
        result = JSON.toJSONStringWithDateFormat(userlist, "yyyy-MM-dd");
        return SUCCESS;
    }

}
